package stark.app.function.endic;

import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.stark.endic.lib.EnDicModule;
import com.stark.endic.lib.model.EnDataManager;
import com.stark.endic.lib.model.bean.DayLearnRec;
import com.stark.endic.lib.model.bean.EnWord;
import com.stark.endic.lib.model.db.EnDbHelper;
import com.stark.endic.lib.ui.FillWordActivity;
import com.stark.endic.lib.ui.LearnWordActivity;
import com.stark.endic.lib.ui.LetterActivity;
import com.stark.endic.lib.ui.ListenTestActivity;
import com.stark.endic.lib.ui.ListenWordActivity;
import com.stark.endic.lib.ui.PhoneticCompareActivity;
import com.stark.endic.lib.ui.PhoneticListActivity;
import com.stark.endic.lib.ui.RememberWordActivity;
import com.stark.endic.lib.ui.StaredWordActivity;
import com.stark.endic.lib.ui.WordListActivity;
import com.stark.endic.lib.ui.ZwSelWordActivity;

import java.util.List;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import stark.app.R;
import stark.app.databinding.ActivityEndicDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.utils.DialogUtil;

public class EndicDemoActivity extends BaseNoModelActivity<ActivityEndicDemoBinding> {

    @Override
    protected int onCreate() {
        return R.layout.activity_endic_demo;
    }

    @Override
    protected void initView() {
        EnDicModule.init(new EnDicModule.InitCallback() {
            @Override
            public void onInit(boolean success) {
                ToastUtils.showShort(success ? "初始化成功" : "初始化失败");
                observe();
            }
        });

        mDataBinding.btnModifyCount.setOnClickListener(v -> {
            EnDataManager.getInstance().setEverydayCount(15);
            ToastUtils.showShort("修改为了每日学习15个单词");
        });
        mDataBinding.btnLearnWord.setOnClickListener(v -> {
            LearnWordActivity.start(this, false, null);
        });
        mDataBinding.btnZwSelWord.setOnClickListener(v -> {
            if (haveLearnedWords()) {
                ZwSelWordActivity.start(this, false, null);
            } else {
                showTip(() -> {
                    LearnWordActivity.start(this, false, null);
                });
            }
        });
        mDataBinding.btnListenTest.setOnClickListener(v -> {
            if (haveLearnedWords()) {
                ListenTestActivity.start(this, false, null);
            } else {
                showTip(() -> {
                    LearnWordActivity.start(this, false, null);
                });
            }
        });
        mDataBinding.btnFillWord.setOnClickListener(v -> {
            if (haveFillWordLearnedWords()) {
                FillWordActivity.start(this, false, null);
            } else {
                showTip(() -> {
                    LearnWordActivity.start(this, false, null);
                });
            }
        });
        mDataBinding.btnListenWord.setOnClickListener(v -> {
            if (haveLearnedWords()) {
                startActivity(ListenWordActivity.class);
            } else {
                showTip(() -> {
                    LearnWordActivity.start(this, false, null);
                });
            }
        });
        mDataBinding.btnStaredWord.setOnClickListener(v -> {
            startActivity(StaredWordActivity.class);
        });
        mDataBinding.btnWordList.setOnClickListener(v -> {
            startActivity(WordListActivity.class);
        });
        mDataBinding.btnRememberWord.setOnClickListener(v -> {
            startActivity(RememberWordActivity.class);
        });

        mDataBinding.btnLetter.setOnClickListener(v -> {
            startActivity(LetterActivity.class);
        });
        mDataBinding.btnPhoneticCompare.setOnClickListener(v -> {
            startActivity(PhoneticCompareActivity.class);
        });
        mDataBinding.btnPhoneticList.setOnClickListener(v -> {
            startActivity(PhoneticListActivity.class);
        });
    }

    @Override
    protected void initData() {
    }

    private void observe() {
        LiveData<DayLearnRec> liveData = EnDataManager.getInstance().getTodayLearnRec();
        liveData.observe(this, new Observer<DayLearnRec>() {
            @Override
            public void onChanged(DayLearnRec rec) {
                if (rec == null) return;

                StringBuilder builder = new StringBuilder();
                builder.append("数据信息：\n");
                List<Integer> idList = rec.getLearnedWordIdList();

                //所有单词总数
                int totalCount = EnDbHelper.getTotalCount();
                builder.append("所有单词总数：" + totalCount + "\n");

                //获取所有已学单词数量
                List<EnWord> learnedWordList = EnDataManager.getInstance().getAllLearnedWords();
                int allLearnedCount = learnedWordList == null ? 0 : learnedWordList.size();
                builder.append("已学单词数：" + allLearnedCount + "\n");

                //未学单词数量
                int noLearnCount = totalCount - allLearnedCount;
                builder.append("未学单词数：" + noLearnCount + "\n");

                //今日已学个数
                int todayLearnedCount = idList == null ? 0 : idList.size();
                builder.append("今日已学单词数：" + todayLearnedCount + "\n");

                //错题个数
                idList = EnDataManager.getInstance().getErrWordIdList();
                int errCount = idList == null ? 0 : idList.size();
                builder.append("错题数：" + errCount + "\n");


                //每日学习个数
                int everydayCount = EnDataManager.getInstance().getEverydayCount();
                builder.append("每日计划学单词数：" + everydayCount + "\n");

                mDataBinding.tvCountInfo.setText(builder.toString());
            }
        });
    }

    /**
     * 判断是否有已学过的单词
     *
     * @return
     */
    private boolean haveLearnedWords() {
        List<EnWord> wordList = EnDataManager.getInstance().getAllLearnedWords();
        return wordList != null && wordList.size() > 0;
    }

    /**
     * 判断是否有适合填空拼写的已学过的单词
     *
     * @return
     */
    private boolean haveFillWordLearnedWords() {
        List<EnWord> wordList = EnDataManager.getInstance().getFillWordFromAllLearnedWords();
        return wordList != null && wordList.size() > 0;
    }

    private void showTip(OnConfirmListener listener) {
        DialogUtil.asConfirm(this, "提示", "您还未学过单词，先去学习新单词吧", "取消", "确定",
                listener, null, false);

    }

}